package com.fyt.reachgo.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import org.springframework.amqp.rabbit.annotation.EnableRabbit;


@Configuration
@EnableRabbit
public class RabbitMQConfig {

    // 队列和交换机名称
    public static final String LIKE_QUEUE = "like.queue";
    public static final String LIKE_EXCHANGE = "like.exchange";
    public static final String LIKE_ROUTING_KEY = "like.routing.key";

    public static final String COMMENT_EXCHANGE = "comment.exchange";
    public static final String COMMENT_ADD_QUEUE = "comment.add.queue";
    public static final String COMMENT_ADD_ROUTING_KEY = "comment.add";
    public static final String COMMENT_LIKE_QUEUE = "comment.like.queue";
    public static final String COMMENT_LIKE_ROUTING_KEY = "comment.like";

    public static final String USER_FAVORITE_EXCHANGE = "userFavorite.exchange";
    public static final String USER_FAVORITE_QUEUE = "userFavorite.queue";
    public static final String USER_FAVORITE_ROUTING_KEY = "userFavorite";

    public static final String USER_DISINTEREST_EXCHANGE = "userDisinterest.exchange";
    public static final String USER_DISINTEREST_QUEUE = "userDisinterest.queue";
    public static final String USER_DISINTEREST_ROUTING_KEY = "userDisinterest";

    public static final String TRAVEL_REQUEST_USER_EXCHANGE = "travel_request_user_exchange";
    public static final String TRAVEL_REQUEST_USER_QUEUE = "travel_request_user_queue";
    public static final String TRAVEL_REQUEST_USER_ROUTING_KEY = "travel_request_user";

    // 路书点赞队列
    @Bean
    public Queue likeQueue() {
        return new Queue(LIKE_QUEUE, true);
    }
    // 路书评论队列
    @Bean
    public Queue commentAddQueue() {
        return new Queue(COMMENT_ADD_QUEUE, true);
    }
    // 路书评论点赞队列
    @Bean
    public Queue commentLikeQueue() {
        return new Queue(COMMENT_LIKE_QUEUE, true);
    }
    // 路书收藏队列
    @Bean
    public Queue userFavoriteQueue() {
        return new Queue(USER_FAVORITE_QUEUE, true);
    }
    // 不感兴趣队列
    @Bean
    public Queue userDisinterestQueue() {
        return new Queue(USER_DISINTEREST_QUEUE, true);
    }
    @Bean
    public Queue travelRequestUserQueue() {
        return new Queue(TRAVEL_REQUEST_USER_QUEUE, true);
    }

    // 创建路书点赞交换机
    @Bean
    public DirectExchange likeExchange() {
        return new DirectExchange(LIKE_EXCHANGE);
    }
    // 创建路书评论交换机
    @Bean
    public TopicExchange commentExchange() {
        return new TopicExchange(COMMENT_EXCHANGE);
    }
    // 收藏交换机
    @Bean
    public TopicExchange userFavoriteExchange() {
        return new TopicExchange(USER_FAVORITE_EXCHANGE);
    }
    // 不感兴趣交换机
    @Bean
    public TopicExchange userDisinterestExchange() {
        return new TopicExchange(USER_DISINTEREST_EXCHANGE);
    }
    @Bean
    public TopicExchange travelRequestUserExchange() {
        return new TopicExchange(TRAVEL_REQUEST_USER_EXCHANGE);
    }


    // 绑定队列到交换机
    // 路书点赞队列绑定到交换机
    @Bean
    public Binding bindingLike() {
        return BindingBuilder.bind(likeQueue())
                .to(likeExchange())
                .with(LIKE_ROUTING_KEY);
    }
    // 路书评论队列绑定到交换机
    @Bean
    public Binding commentAddBinding() {
        return BindingBuilder.bind(commentAddQueue())
                .to(commentExchange())
                .with(COMMENT_ADD_ROUTING_KEY);
    }
    // 路书评论点赞队列绑定到交换机
    @Bean
    public Binding commentLikeBinding() {
        return BindingBuilder.bind(commentLikeQueue())
                .to(commentExchange())
                .with(COMMENT_LIKE_ROUTING_KEY);
    }
    @Bean
    public Binding userFavoriteBinding() {
        return BindingBuilder.bind(userFavoriteQueue())
                .to(userFavoriteExchange())
                .with(USER_FAVORITE_ROUTING_KEY);
    }
    @Bean
    public Binding userDisinterestBinding() {
        return BindingBuilder.bind(userDisinterestQueue())
                .to(userDisinterestExchange())
                .with(USER_DISINTEREST_ROUTING_KEY);
    }
    @Bean
    public Binding bindingTravelRequestUser() {
        return BindingBuilder.bind(travelRequestUserQueue())
                .to(travelRequestUserExchange())
                .with(TRAVEL_REQUEST_USER_ROUTING_KEY);
    }




    // JSON消息转换器
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 配置RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    // 配置监听器容器工厂
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory,
            MessageConverter jsonMessageConverter) {

        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter);

        // 设置确认模式为手动确认
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);

        // 配置重试策略
        factory.setAdviceChain(
                RetryInterceptorBuilder.stateless()
                        .maxAttempts(3)
                        .backOffOptions(1000, 2.0, 10000)
                        .build()
        );

        // 配置并发
        factory.setConcurrentConsumers(5);
        factory.setMaxConcurrentConsumers(10);
        factory.setPrefetchCount(20);

        return factory;
    }
}
